<template>
  <div class="content-wrap">
    <el-row class="btnWrap">
      <el-button
        icon="el-icon-plus"
        class="btn left-btn"
        type="primary"
        @click="showInput"
      >
        新增
      </el-button>
    </el-row>

    <el-table fit border :data="tableList" height="500" stripe>
      <el-table-column
        label="课程id"
        align="center"
        width="80px"
        header-align="center"
        prop="subjectId"
      ></el-table-column>
      <el-table-column
        label="课程名"
        align="center"
        header-align="center"
        prop="subjectName"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="editListItem(item.$index, item.row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteListItem(item.$index, item.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="isShowEditAlert">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="课程名" prop="className">
          <el-input v-model="form.subjectName" placeholder=""></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="success" @click="submitForm"> 提交 </el-button>
        <el-button type="success" @click="submitClose1"> 取消 </el-button>
      </template>
    </el-dialog>

    <el-dialog title="提示" width="30%" center :visible.sync="isShowEditAdd">
      <b>请确认是否提交！</b>
      <template slot="footer">
        <el-button type="success" @click="submitFormAdd"> 确认 </el-button>
        <el-button type="success" @click="submitClose2"> 取消 </el-button>
      </template>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="isShowEditDialog">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="课程名" prop="subjectName">
          <el-input v-model="formModify.subjectName" placeholder=""></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="success" @click="submitFormDialog"> 提交 </el-button>
        <el-button type="success" @click="submitClose1"> 取消 </el-button>
      </template>
    </el-dialog>
    <el-dialog title="提示" width="30%" center :visible.sync="isShowEditModify">
      <b>请确认是否修改！</b>
      <template slot="footer">
        <el-button type="success" @click="submitFormModify"> 确认 </el-button>
        <el-button type="success" @click="submitClose2"> 取消 </el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "StudentInfo",
  data() {
    return {
      teac_list:[],
      tableList: [],
      isShowDeleteAlert: false,
      //增加
      isShowEditAlert: false,
      isShowEditAdd: false,
      //修改
      isShowEditDialog: false,
      isShowEditModify: false,
      page: {
        PageTotal: 5,
        index: 1,
        PageSize: 8,
      },
      allDevList: [],
      form: {},
      formModify: {},
    };
  },

  methods: {
    //新增 add
    submitFormAdd() {
      let that = this;
      that.axios
        .post("subject/addSubject.php", {
          subjectName: that.form.subjectName,
        })
        .then(function (res) {
          console.log(res);
          if(res.data.state === 'success'){
            that.$message({
              message:'添加成功',
              type:'success'
            })
          that.GetAllStudent();
          }else{
            that.$message({
              message:'添加失败:' + res.data.err,
              type:'error'
            })
            that.isShowEditAdd = false
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    //删除 delete
    deleteListItem(index) {
      let that = this;
      let subjectId = that.tableList[index].subjectId;
      that.axios
        .post("subject/delSubject.php",{
          subjectId,
        })
        .then(function (res) {
          console.log(res);
          if(res.data.state === 'success'){
            that.$message({
              message:'删除成功',
              type:'success'
            })
          that.GetAllStudent();
          }else{
            that.$message({
              message:'删除失败:' + res.data.err,
              type:'error'
            })
          }
        })
    },
    //编辑 modify
    submitFormModify() {
      let that = this;
      console.log(that.formModify);
      that.axios
        .post("subject/editSubject.php", {
          subjectName: that.formModify.subjectName,
          subjectId: that.formModify.subjectId,
        })
        .then(function (res) {
          console.log(res);
          if(res.data.state === 'success'){
            that.$message({
              message:'修改成功',
              type:'success'
            })
          that.GetAllStudent();
          }else{
            that.$message({
              message:'修改失败:' + res.data.err,
              type:'error'
            })
            that.isShowEditModify = false
          }
        })
    },
    //刷新
    GetAllStudent() {
      let that = this;
      that.axios
        .get("subject/getAllSubject.php", {})
        .then(function (res) {
          console.log(res);
          that.tableList = res.data.data
          that.isShowDeleteAlert = false;
          that.isShowEditAlert = false;
          that.isShowEditAdd = false;
          that.isShowEditDialog = false;
          that.isShowEditModify = false;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    changePage(index) {
      let that = this;
      that.page.index = index;
      that.GetAllStudent();
    },
    //控制dialog出现消失
    showInput() {
      let that = this;
      that.isShowEditAlert = true;
    },
    getTeacList(){
      let that = this
      this.axios
      .get("/opt/teacher",{})
      .then(res=>{
        console.log(res.data.data);
          that.teac_list = res.data.data
      })
    },
    submitForm() {
      let that = this;
      that.isShowEditAdd = true;
    },
    submitClose1() {
      let that = this;
      that.isShowEditAlert = false;
      that.isShowEditDialog = false;
    },
    submitClose2() {
      let that = this;
      that.isShowEditAdd = false;
      that.isShowEditModify = false;
    },
    editListItem(index) {
      let that = this;
      that.isShowEditDialog = true;
      that.formModify = JSON.parse(JSON.stringify(that.tableList[index]));
      console.log(that.formModify);
    },
    submitFormDialog() {
      let that = this;
      that.isShowEditModify = true;
    },
  },
  mounted() {
    let that = this;
    that.getTeacList();
    that.GetAllStudent();
  },
};
</script>

<style scoped>
.content-wrap .btn {
  position: absolute;
  z-index: 100;
}
.content-wrap .btnWrap {
  position: relative;
  height: 50px;
}
.content-wrap .left-btn {
  left: 0;
}
.content-wrap .right-btn {
  right: 0;
}
</style>